﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYRElectric.WirePlace;

namespace HYRElectric.Equipment
{
	public partial class FanLayoutForm : System.Windows.Forms.Form
	{
		public FanLayoutForm(Document document)
		{
			this.InitializeComponent();
			this.doc = document;
		}

		private void FanLayoutForm_Load(object sender, EventArgs e)
		{
			this.InitForm();
			this.ReadFormDataFromXml();
		}

		private void InitForm()
		{
			//base.Icon = Resource.YArchitechIcon;
			this.txtRCount.MostValue = new MostValue(1.0, 100.0);
			this.txtCCount.MostValue = new MostValue(1.0, 100.0);
			this.txtMargin.MostValue = new MostValue(0.0, 1.0);
			this.txtAngle.MostValue = new MostValue(0.0, 360.0);
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\Fan";
			xmlHelper.SelectNode(path);
			string attributeEx = xmlHelper.GetAttributeEx("RCount", "3");
			this.txtRCount.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("CCount", "3");
			this.txtCCount.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("Margin", "0.5");
			this.txtMargin.Text = attributeEx;
			bool @checked = Convert.ToBoolean(xmlHelper.GetAttributeEx("IsAutoAngle", "true"));
			this.checkAngle.Checked = @checked;
			this.UpdateAngleTextbox();
			attributeEx = xmlHelper.GetAttributeEx("Angle", "0");
			this.txtAngle.Text = attributeEx;
			int num = Convert.ToInt32(xmlHelper.GetAttributeEx("DislocationWay", "0"));
			if (num == 0)
			{
				this.radioNone.Checked = true;
			}
			else if (num == 1)
			{
				this.radioR.Checked = true;
			}
			else
			{
				this.radioC.Checked = true;
			}
			num = Convert.ToInt32(xmlHelper.GetAttributeEx("LinkWay", "0"));
			if (num == 0)
			{
				this.radioNoneLine.Checked = true;
			}
			else if (num == 1)
			{
				this.radioRLine.Checked = true;
			}
			else
			{
				this.radioCLine.Checked = true;
			}
			this.UpdateWireControl();
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
			base.Location = new System.Drawing.Point(x, y);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\Fan";
			xmlHelper.SelectNode(path);
			xmlHelper.SetAttribute("RCount", this.RCount.ToString());
			xmlHelper.SetAttribute("CCount", this.CCount.ToString());
			xmlHelper.SetAttribute("Margin", this.Margin.ToString());
			xmlHelper.SetAttribute("IsAutoAngle", this.IsAutoAngle.ToString());
			xmlHelper.SetAttribute("Angle", this.Angle.ToString());
			xmlHelper.SetAttribute("DislocationWay", this.DislocationWay.ToString());
			xmlHelper.SetAttribute("LinkWay", this.LinkLineWay.ToString());
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml("");
		}

		private void UpdateAngleTextbox()
		{
			this.labelAngle.Enabled = !this.checkAngle.Checked;
			this.txtAngle.Enabled = !this.checkAngle.Checked;
		}

		private void UpdateWireControl()
		{
			this.buttonWire.Enabled = !this.radioNoneLine.Checked;
		}

		private void checkAngle_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateAngleTextbox();
		}

		private void radioNone_CheckedChanged(object sender, EventArgs e)
		{
			this.radioRLine.Enabled = !this.radioC.Checked;
		}

		private void radioNoneLine_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateWireControl();
		}

		private void buttonWire_Click(object sender, EventArgs e)
		{
			new NewWireParamForm(this.doc).ShowDialog();
		}

		private void FanLayoutForm_Deactivate(object sender, EventArgs e)
		{
			string text = this.txtRCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtRCount.Text = this.txtRCount.OldValue;
			}
			text = this.txtRCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtRCount.Text = "3";
			}
			text = this.txtCCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtCCount.Text = this.txtCCount.OldValue;
			}
			text = this.txtCCount.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtCCount.Text = "3";
			}
			text = this.txtMargin.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtMargin.Text = this.txtMargin.OldValue;
			}
			text = this.txtMargin.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtMargin.Text = "0.5";
			}
			text = this.txtAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtAngle.Text = this.txtAngle.OldValue;
			}
			text = this.txtAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtAngle.Text = "0";
			}
		}

		private void FanLayoutForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveFormDataToXml();
		}

		protected override void WndProc(ref Message msg)
		{
			if (msg.Msg == 274 && (int)msg.WParam == 61536)
			{
				base.Hide();
				return;
			}
			base.WndProc(ref msg);
		}

		public int RCount
		{
			get
			{
				return Convert.ToInt32(this.txtRCount.Text.Trim());
			}
		}

		public int CCount
		{
			get
			{
				return Convert.ToInt32(this.txtCCount.Text.Trim());
			}
		}

		public new double Margin
		{
			get
			{
				return Convert.ToDouble(this.txtMargin.Text.Trim());
			}
		}

		public bool IsAutoAngle
		{
			get
			{
				return this.checkAngle.Checked;
			}
		}

		public int Angle
		{
			get
			{
				return Convert.ToInt32(this.txtAngle.Text.Trim());
			}
		}

		public int DislocationWay
		{
			get
			{
				if (this.radioC.Checked)
				{
					return 2;
				}
				if (this.radioR.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		public int LinkLineWay
		{
			get
			{
				if (this.radioCLine.Checked)
				{
					return 2;
				}
				if (this.radioRLine.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		private const string XmlNode_Root = "Fan";

		private const string XmlNode_RCount = "RCount";

		private const string XmlNode_CCount = "CCount";

		private const string XmlNode_Margin = "Margin";

		private const string XmlNode_IsAutoAngle = "IsAutoAngle";

		private const string XmlNode_Angle = "Angle";

		private const string XmlNode_DislocationWay = "DislocationWay";

		private const string XmlNode_LinkWay = "LinkWay";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private Document doc;
	}
}
